package com.lwj.liwanjia.view.activity.shop.food;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.RhxhAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.SpBean;
import com.lwj.liwanjia.impl.SpView;
import com.lwj.liwanjia.presenter.SpPresenter;
import com.lwj.liwanjia.utils.GlideImageLoader;
import com.lwj.liwanjia.utils.PopUtils;
import com.lwj.liwanjia.view.activity.shop.rihuaxihu.RhxhTypeActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 食品
 */
public class FoodActivity extends BaseActivity implements SpView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.rlv_rhxh)
    SuperRecyclerView rlvRhxh;
    @BindView(R.id.progress)
    ProgressBar prgressBar;

    private RhxhAdapter adapter;
    private List<SpBean.DataBean.RefferBean> list = new ArrayList<>();
    private Banner banner;
    private List<String> imageList = new ArrayList<>();
    private String webShopId;
    private SpPresenter presenter;

    @Override
    protected void initView() {
        webShopId = getIntent().getStringExtra("webShopId");
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("食品");
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.fenlei);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvRhxh.setLayoutManager(manager);
        rlvRhxh.setRefreshEnabled(false);
        rlvRhxh.setLoadMoreEnabled(false);
        adapter = new RhxhAdapter(this, list);
        addView();
        rlvRhxh.setAdapter(adapter);
        presenter = new SpPresenter(this);
        listener();
    }

    private void listener() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(FoodActivity.this, RhxhTypeActivity.class);
                intent.putExtra("msg_type_id", list.get(position).getMsg_type_id());
                startActivity(intent);
            }
        });
    }

    private void addView() {
        View view = LayoutInflater.from(this).inflate(R.layout.banner_head, null);
        banner = (Banner) view.findViewById(R.id.product_banner);
        LinearLayout tuijian = (LinearLayout) view.findViewById(R.id.rl_tuijian);
        tuijian.setVisibility(View.VISIBLE);
        adapter.addHeaderView(view);
    }

    @Override
    protected void initData() {
        presenter.getSpData(webShopId);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rhxh;
    }

    @OnClick({R.id.tv_left, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.iv_right:
                PopUtils.getInstance().showPopupWindow(this, ivRight);
                break;
        }
    }

    @Override
    public void showProgress() {
        prgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        prgressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getSpData(SpBean spBean) {
        if (spBean.getState() == 0) {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (spBean.getState() == 1) {
            imageList.addAll(spBean.getData().getSlideImgs());
            banner.setImages(imageList)
                    .setImageLoader(new GlideImageLoader())
                    .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                    .setDelayTime(4000)
                    .start();
            list.clear();
            list.addAll(spBean.getData().getReffer());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
    }
}
